const model = require('../models/spiderModel'),
  errCode = require('../../conf/errorCode'),
  wrap = require('../helper/wrap'),
  util = require('../helper/util');

exports.addBrand = function(
  {capital, autohomeId, brandName, brandLogoUrl},
  callback,
) {
  var tmp = util.firstKeyOfNull(
    {
      capital,
      autohomeId,
      brandName,
      brandLogoUrl,
    },
    ['capital', 'autohomeId', 'brandName', 'brandLogoUrl'],
  );
  if (tmp != null) {
    return callback({errNo: errCode.LESS_OF_DATA_ERROR, extraMsgs: tmp});
  }
  model.addBrand(
    wrap.changeData(
      {capital, autohomeId, brandName, brandLogoUrl},
      wrap.names.carBrand,
      wrap.names.front,
      wrap.names.end,
    ),
    (err, result) => {
      if (err) return callback(err);
      return callback(null, {id: result.insertId});
    },
  );
};

exports.getBrandIdFromAutoHomeId = function(autohomeId,callback){
    model.getBrandIdFromAutoHomeId(autohomeId,callback);    
}

exports.addModel = function({brandId,autohomeId, manufacturer, modelName}, callback) {
  var tmp = util.firstKeyOfNull(
    {
      brandId,
      autohomeId,
      manufacturer,
      modelName,
    },
    ['brandId','autohomeId', 'manufacturer', 'modelName'],
  );
  if (tmp != null) {
    return callback({errNo: errCode.LESS_OF_DATA_ERROR, extraMsgs: tmp});
  }
  model.addModel(
    wrap.changeData(
      {brandId,autohomeId, manufacturer, modelName},
      wrap.names.carModel,
      wrap.names.front,
      wrap.names.end,
    ),
    (err, result) => {
      if (err) return callback(err);
      return callback(null, {id: result.insertId});
    },
  );
};
